import { getTerrHightByDegree } from '../../tools'
export default {
    name: 'MixinPopStay',
    data () {
        return {
            posX: 0,
            posY: 0
        }
    },
    computed: {
        PosX () {
            return this.$store.getters.popStayParams.posX
        },
        PosY () {
            return this.$store.getters.popStayParams.posY
        }
    },
    beforeDestroy () {
        this.$viewer.clock.onTick.removeEventListener(this.posEvent)
    },
    mounted () {
        this.$nextTick(() => {
            this.setX(this.$store.getters.popStayParams.posX)
            this.setY(this.$store.getters.popStayParams.posY)
            this.posEvent = clock => {
                // console.log(this.remarks)
                var lgtd = Number(this.remarks.Lgtd || this.remarks.lgtd)
                var lttd = Number(this.remarks.Lttd || this.remarks.lttd)
                var h = this.remarks.h
                // 如果有地形
                if (this.$viewer.scene.terrainProvider.availability) {
                    // 如果有现成的高度
                    if (h) {
                        this.setXY(lgtd, lttd, h)
                    } else {
                        // 如果地形开启，没有存在高度就去算，实时算会有卡顿，最好预存
                        getTerrHightByDegree(this.$viewer, lgtd, lttd).then(res => {
                            this.remarks.h = res
                            this.setXY(lgtd, lttd, res)
                        })
                    }
                } else {
                    this.setXY(lgtd, lttd, 0)
                }
            }
            this.$viewer.clock.onTick.addEventListener(this.posEvent)
        })
    },
    watch: {
        PosX (val) {
            this.setX(val)
        },
        PosY (val) {
            this.setY(val)
        }
    },
    methods: {
        setXY (lgtd, lttd, h) {
            const position = Cesium.SceneTransforms.wgs84ToWindowCoordinates(
                this.$viewer.scene,
                Cesium.Cartesian3.fromDegrees(
                    lgtd,
                    lttd,
                    h
                )
            )
            if (position) {
                this.setX(position.x)
                this.setY(position.y)
            }
        },
        setX (val) {
            this.$nextTick(() => {
                var div = document.getElementsByClassName('popStay-wrap ')[0]
                if (div) {
                    var divW = div.clientWidth
                    this.posX = val - divW / 2
                }
            })
        },
        setY (val) {
            this.$nextTick(() => {
                var div = document.getElementsByClassName('popStay-wrap ')[0]
                if (div) {
                    var divH = div.clientHeight
                    this.posY = val - divH
                }
            })
        }
    }
}

